package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 *内容合理性
 * 全文
 * 全篇检查金额单位是否为千分位；
 * 预评/发行阶段
 */
@Component("R1011")
@Scope("prototype")
public class R1011 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1011.class);
    @Autowired
    private HandingData handingData;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1011组件M");
        List<ComponentResult> componentResults = checkRulesR1011(getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1011组件M");
        List<ComponentResult> componentResults = checkRulesR1011(getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1011组件DM");
        List<ComponentResult> componentResults = checkRulesR1011(getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1011组件M");
        List<ComponentResult> componentResults = checkRulesR1011(getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }


    private List<ComponentResult> checkRulesR1011(String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        boolean flag = false;
        int startPage = 0;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        try {

            if (extractData!=null){
                List<KVText> noThousandSeparatorList = extractData.getNoThousandSeparatorList();
                if (CollectionUtils.isNotEmpty(noThousandSeparatorList)){
                    String qff = null;
                    for (KVText text : noThousandSeparatorList){
                         qff = text.getText();
                        pageNum = text.getPageNumber();
                        pageNum = pageNum!=0?pageNum:1;
                        contentAndPositionList = getContentAndPositionList(text);
                        componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"千分符检查-金额单位为:"+qff,"缺少数字千分符",true,getRegulationId(),pageNum,contentAndPositionList,"02");
                        componentResults.add(componentResult);
                    }
                }
            }
            if (CollectionUtils.isEmpty(componentResults)){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"千分符检查","未检查到金额单位缺少千分符,建议人工核对",true,getRegulationId(),1,contentAndPositionList,"03");
                componentResults.add(componentResult);
            }
        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"千分符检查","未识别,建议人工核对",true,getRegulationId(),1,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }
        return componentResults;
    }
}
